import { Column, Entity, ManyToOne, OneToMany, Index } from 'typeorm';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Novel } from './novel.entity';
import { UserReadingHistory } from './user-reading-history.entity';

@Entity('novel_chapters')
@Index(['novelId'])
@Index(['novelId', 'chapterNumber'], { unique: true })
export class NovelChapter extends BaseEntity {
  @Column({ type: 'bigint', comment: '小说ID' })
  novelId: number;

  @Column({ type: 'varchar', length: 200, comment: '章节标题' })
  title: string;

  @Column({ type: 'longtext', comment: '章节内容' })
  content: string;

  @Column({ type: 'int', comment: '章节序号' })
  chapterNumber: number;

  @Column({ type: 'int', default: 0, comment: '字数' })
  wordCount: number;

  @Column({
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '章节URL',
  })
  chapterUrl: string | null;

  @Column({
    type: 'tinyint',
    default: 1,
    comment: '是否免费: 0-收费 1-免费',
  })
  isFree: number;

  @Column({
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: '卷名',
  })
  volumeName: string | null;

  // 关联关系
  @ManyToOne(() => Novel, (novel) => novel.chapters, { onDelete: 'CASCADE' })
  novel: Novel;

  @OneToMany(() => UserReadingHistory, (history) => history.chapter)
  readingHistory: UserReadingHistory[];
}
